package com.zhang.second.day06;

import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.ProcessFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.streaming.api.functions.windowing.ProcessWindowFunction;
import org.apache.flink.streaming.api.watermark.Watermark;
import org.apache.flink.streaming.api.windowing.assigners.TumblingEventTimeWindows;
import org.apache.flink.streaming.api.windowing.time.Time;
import org.apache.flink.streaming.api.windowing.windows.TimeWindow;
import org.apache.flink.util.Collector;
import org.apache.flink.util.OutputTag;

/**
 * @title: 处理迟到数据：默认策略
 * @author: zhang
 * @date: 2022/2/1 18:43
 */
public class Example4 {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);

        SingleOutputStreamOperator<String> result = env
                .addSource(new SourceFunction<Integer>() {
                    @Override
                    public void run(SourceContext<Integer> ctx) throws Exception {
                        ctx.collectWithTimestamp(1, 1000L);
                        Thread.sleep(1000L);
                        ctx.emitWatermark(new Watermark(999L));
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(2, 2000L);
                        Thread.sleep(1000L);
                        ctx.emitWatermark(new Watermark(1999L));
                        Thread.sleep(1000L);

                        ctx.emitWatermark(new Watermark(5000L));
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(3, 4000L);
                        Thread.sleep(1000L);

                        ctx.collectWithTimestamp(4, 6000L);
                        Thread.sleep(1000L);
                    }

                    @Override
                    public void cancel() {

                    }
                })
                .keyBy(r -> 1)
                .window(TumblingEventTimeWindows.of(Time.seconds(5)))
                .sideOutputLateData(new OutputTag<Integer>("later") {
                })
                .process(new ProcessWindowFunction<Integer, String, Integer, TimeWindow>() {
                    @Override
                    public void process(Integer integer, ProcessWindowFunction<Integer, String, Integer, TimeWindow>.Context context, Iterable<Integer> elements, Collector<String> out) throws Exception {
                        out.collect("窗口共有" + context.window().getStart() + "~" + context.window().getEnd() +"->"+ elements.spliterator().getExactSizeIfKnown() + "个元素");
                    }
                });

        result.print();
        result.getSideOutput(new OutputTag<Integer>("later") {
        }).print("迟到元素：");

        env.execute();
    }
}
